<template>
	<view class="pwd-retrieve-container">
		<uni-forms ref="form" :value="user" labelWidth="80px">
			<uni-forms-item name="code" label="验证码">
				<view class="input-code"><uni-easyinput type="password" v-model="user.code" placeholder="请输入验证码" />
				</view>
				<view class="button-code">
					<button type="primary" class="button-code-img" :disabled="codeButtonDisabled"
						@tap="sendVerificationCode">{{ codeButtonText }}</button>
				</view>
			</uni-forms-item>
			<uni-forms-item name="password" label="新密码">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item name="confirmPassword" label="确认密码">
				<uni-easyinput type="password" v-model="user.confirmPassword" placeholder="请确认新密码" />
			</uni-forms-item>
			<button type="primary" @click="submit">提交</button>
		</uni-forms>
	</view>
</template>

<script setup>
	import {
		updateUserPwd,
		updateUserPwdCode
	} from "@/pages/api/system/user"
	import {
		onReady
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance,
	} from 'vue'
	const {
		proxy
	} = getCurrentInstance();
	const user = ref({
		uuid: undefined,
		code: undefined,
		password: undefined,
		confirmPassword: undefined
	});
	const rules = ref({
		code: {
			rules: [{
				required: true,
				errorMessage: '验证码不能为空'
			}]
		},
		password: {
			rules: [{
					required: true,
					errorMessage: '新密码不能为空',
				},
				{
					minLength: 6,
					maxLength: 20,
					errorMessage: '长度在 6 到 20 个字符'
				}
			]
		},
		confirmPassword: {
			rules: [{
				required: true,
				errorMessage: '确认密码不能为空'
			}, {
				validateFunction: (rule, value, data) => data.password === value,
				errorMessage: '两次输入的密码不一致'
			}]
		}
	})

	// 验证码相关
	const codeButtonDisabled = ref(false)
	const codeButtonText = ref('获取验证码')
	let countdownTimer = null
	let seconds = 60

	// 发送验证码
	const sendVerificationCode = async () => {
		try {
			// 模拟发送验证码请求
			codeButtonDisabled.value = true
			countdownTimer = setInterval(() => {
				codeButtonText.value = `${seconds}秒后重试`
				seconds--
				if (seconds < 0) {
					clearInterval(countdownTimer)
					codeButtonDisabled.value = false
					codeButtonText.value = '获取验证码'
					seconds = 60
				}
			}, 1000)

			// 这里可以替换为真实的API调用
			updateUserPwdCode().then(res => {
				user.value.uuid = res.data.uuid
				uni.showToast({
					icon: 'none',
					title: '验证码已发送，请注意查收'
				})
			})
		} catch (error) {
			uni.showToast({
				icon: 'none',
				title: '发送验证码失败'
			})
			console.error(error)
		}
	}

	onReady(() => {
		proxy.$refs.form.setRules(rules.value)
	})

	const submit = () => {
		proxy.$refs.form.validate().then(res => {
			updateUserPwd(user.value).then(response => {
				proxy.$modal.msgSuccess("修改成功")
				setTimeout(function(){
					uni.navigateBack()
				},2000)
			})
		})
	}
</script>

<style lang="scss" scoped>
	.pwd-retrieve-container {
		padding-top: 36rpx;
		padding: 15px;
	}

	.input-code {
		display: inline-block;
		width: 60%;
		height: 38px;
		float: left;
	}

	.button-code {
		display: inline-block;
		width: 40%;
		height: 38px;
		float: right;

		.button-code-img {
			height: 38px;
			position: absolute;
			margin-left: 10px;
			width: 200rpx;
			font-size: 30rpx;
		}
	}
</style>

<style lang="scss">
	//仅设置背景，不推荐在这里写样式代码
	page {
		background-color: #f8f8f8;
	}
</style>